package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.recharge.QueryRechargeRecordFormBean;
import com.info.zhiduoduo.admin.model.formbean.recharge.SaveRechargeRecordFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.recharge.RechargeRecordDto;
import com.info.zhiduoduo.repository.entity.recharge.RechargeRecord;
import com.info.zhiduoduo.repository.entity.recharge.RechargeType;
import com.info.zhiduoduo.repository.mapper.recharge.RechargeRecordMapper;
import com.info.zhiduoduo.repository.mapper.recharge.RechargeTypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tianyonghong
 * @create 2021/12/9
 */
@Service
public class RechargeService extends BaseService {

    @Resource
    private RechargeRecordMapper rechargeRecordMapper;

    @Resource
    private RechargeTypeMapper rechargeTypeMapper;

    /**
     * 查询收费记录
     * @param formBean
     * @return
     */
    public SmartResult<RechargeRecordDto> queryRechargeRecordList(BaseSimpleFormBean<QueryRechargeRecordFormBean> formBean){
        QueryRechargeRecordFormBean reqdata = formBean.getReqdata();

        Page<RechargeRecordDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        rechargeRecordMapper.queryRechargeRecordList(reqdata.getCustomerName(),reqdata.getCustomerType(),reqdata.getStartTime(),reqdata.getEndTime());

        return SmartResult.ofPage(page);
    }

    /**
     * 新增收费记录
     * @param formBean
     * @return
     */
    public SmartResult saveOrUpdateRechargeRecord(BaseSimpleFormBean<SaveRechargeRecordFormBean> formBean) {
        SaveRechargeRecordFormBean reqdata = formBean.getReqdata();

        RechargeRecord rechargeRecord = new RechargeRecord();
        rechargeRecord.setCreatedBy(formBean.getUserid());
        BeanUtils.copyProperties(reqdata, rechargeRecord);

        boolean result = rechargeRecord.insertOrUpdate();
        if (!result) {
            return SmartResult.fail();
        }
        return SmartResult.success();
    }

    /**
     * 删除收费记录
     * @param rechargeId
     * @return
     */
    public SmartResult delRechargeRecord(Integer rechargeId) {

        RechargeRecord rechargeRecord = new RechargeRecord();
        rechargeRecord.setRechargeId(rechargeId);
        boolean result = rechargeRecord.deleteById();

        if (!result) {
            return SmartResult.fail();
        }
        return SmartResult.success();
    }

    public SmartResult<RechargeType> queryAllRechargeType(BaseSimpleFormBean baseSimpleFormBean) {
        List<RechargeType> list = rechargeTypeMapper.selectList(null);
        return SmartResult.success(list);
    }
}
